<?php

namespace App\Admin\Actions\Auction;

use App\Admin\AdminUpload;
use App\Library\Logic\MediaRelationLogic;
use App\Library\Logic\OperateLogLogic;
use App\Models\AuctionModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Encore\Admin\Actions\RowAction;
use OSS\Core\OssException;
use OSS\Http\RequestCore_Exception;

class AuctionUploadImage extends RowAction
{
    public $name = '上传图片';

    /**
     * handle
     * @throws RequestCore_Exception
     * @throws OssException
     */
    public function handle(AuctionModel $model, Request $request): Response
    {
        $fileId = AdminUpload::upload($request->file('file'))->serial_number;
        MediaRelationLogic::insertForModel([
            'fileId' => $fileId,
            'objectType' => 1,
            'objectId' => $model->id,
            'type' => 0,
        ]);
        if (empty($model->file_id)) $model->file_id = $fileId;
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.auction.uploadImg', ['fileId' => $fileId]);
        return $this->response()->success('操作成功')->refresh();
    }

    public function form(): void
    {
        $this->image('file', '上传图片');
    }
}
